package sshFile;


import org.apache.commons.net.telnet.TelnetClient;

import java.io.*;


public class TelnetTool2 {
	private TelnetClient telnet = new TelnetClient();
	private PrintStream out;
	private InputStream in;
	private char prompt = '$';
    public InputStreamReader inre;
    
	// 闁哄拋鍣ｉ敓鑺ユ皑閺併倝骞嬫搴ｆ尝闁哄鎷�
	public String runOmc(String ip, int port, String user, String password, String fileName) {
		try {
			telnet.connect(ip, port);
			System.out.println("telnet执行");

			out = new PrintStream(telnet.getOutputStream(),false,"utf-8");
		
			// 闁哄秷顫夊畵涔簅ot闁烩偓鍔嶉崺娑氭媼閸撗呮瀭缂備焦鎸诲顐ょ箔閿燂拷
			this.prompt = user.equals("root") ? '#' : '$';
			in = telnet.getInputStream();
			inre=new InputStreamReader(in,"gbk");
			write("LGI:OP=\""+user+"\", PWD=\""+password+"\";");
			System.out.println("OMC登陆成功");

		//	write("S_ACTIVATE:FILE=\""+fileName+"\";");
			
			
			

			return readUntil("---    END");
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}


	
	public boolean  testConn(String ip, int port) {
		boolean  flag=false;
		try {
			telnet.connect(ip, port);
			flag=true;
		} catch (Exception e) {
			flag=false;
			e.printStackTrace();
		}
		return flag;
	}

	/** * 闁谎嗩嚙缂嶏拷 * * @param user * @param password */
	public boolean login(String ip, int port,String user, String password) {
		boolean  flag=false;
		try {
			telnet.connect(ip, port);
			out = new PrintStream(telnet.getOutputStream());
			in = telnet.getInputStream();
			//readUntil("login:");
			write(user);
			//readUntil("Password:");
			write(password);
			//readUntil(prompt + " ");
			flag=true;
		} catch (Exception e) {
			flag=false;
			e.printStackTrace();
		}
		return flag;
	}

	/** * 闁告碍鍨瑰ú浼村冀閸パ冪岛闂侇偂绀侀幊鈩冪閵堝懐鎽熺紒妤嬬細鐟曪拷 * * @param command * @return */
	public String sendCommand(String command) {
		try {
			System.out.println("开始输入命令: "+command);
			write(command);
			System.out.println("输入完毕");
			return readUntil("---    END");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/** * 闁告劖鐟﹂幖閿嬫媴閿燂拷 * * @param value */
	public void write(String value) {
		try {
			out.println(value);
			out.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
 
	/** * 闁稿繑濞婂Λ瀛樻交閻愭潙澶� */
	public void disconnect() {
		try {
			System.out.println("开始断开");
			telnet.disconnect();
			System.out.println("已断开");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/** * 閻犲洩顕цぐ鍥礆閸℃锟界晫绱掗幘瀵镐函 * * @param pattern * @return */
	public String readUntil(String pattern) {
		try {
			char lastChar = pattern.charAt(pattern.length() - 1);
			StringBuffer sb = new StringBuffer();
			
			
			
			char ch = (char) inre.read();
			int i = 0;
			while (true) {
				sb.append(ch);
//				if (ch == lastChar) {
//					if (sb.toString().endsWith(pattern)) {
//						return sb.toString();
//					}
//				}
				i++;
				ch = (char) inre.read();
				System.out.println(ch);
				if(i>10)
					break;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/*
	 * 闁告劖鐟ラ崣鍡涘棘閸ワ附顐�
	 * */
	public static void doWrite(String path,String data){
		try {
			System.out.println("开始写txt");
			File file = new File(path);
			Writer ps = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
			
				ps.write(data+"\r\n");
			
			ps.flush();
			ps.close();
			System.out.println("写完");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	
	public static void main(String[] args) {
		try {
			System.out.println("开始执行ash");
			String ip = "132.226.223.79";
			int port = 21123;
			String user = "huqiao.zte";
			String password = "Sophie@135";
			String comm = "HELP";
			System.out.println(comm);
			TelnetTool2 telnet = new TelnetTool2();
			 telnet.runOmc(ip, port, user, password,null);
			 String order =telnet.sendCommand(comm);
			 String path="C:\\new\\omc.txt";
			 telnet.disconnect();

			 doWrite(path,order);
 
			 System.out.println("命令执行结果:"+order);
		//	 CreateCsv.createCSVtool(path);
			
			/* File txtfile=new File(path);
			 if(txtfile.exists()){
			 txtfile.delete();
			 }*/
			
			System.out.println("end ...");
		} catch (Exception e) {
			System.out.println("e:"+e.getMessage());
			e.printStackTrace();
		}
	}
}